<script>
function isMobile() {
    return (/phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone|webOS|android/i.test(navigator.userAgent))
}
var _isMobile=isMobile();
_h=h= Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var sWidth = parseInt((window.innerWidth > 0) ? window.innerWidth : screen.width);
if(sWidth>750){sWidth=750;}
console.log(_isMobile);
//if(sWidth>540){sWidth=540;}
var _rate=(sWidth/750);
if(h<_rate*1206){
  h=_rate*1206;
}
function setKe(cname, cvalue){
  var cname='jk1'+cname;
    var d = new Date();
    d.setTime(d.getTime() + (1000*60*60*24*30)); //15分钟
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getke(cname) {
  var cname='jk1'+cname;
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
         }
         if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
         }
     }
    return "";
}
function chinanum(num){
    var china = new Array('零','一','二','三','四','五','六','七','八','九');  
    var arr = new Array();
    var num=num+'';
    var english = num.split("")  
    for(var i=0;i<english.length;i++){
        arr[i] = china[english[i]];
    }
    if(2 == english.length){
        if('一' == arr[0]){
          if ('零' == arr[1]) {
              return '十';
          }
          return ('十'+arr[1]);
        }else{
          if ('零' == arr[1]) {
               return (arr[0]+'十');
          }
          return (arr[0]+'十'+arr[1]);
        }
    }else{
      return arr.join("")  
    }
}
Array.prototype.shuffle = function(){
    let array = this;
    let len = array.length;
    for (let i = len - 1; i > 0; i--) {
      let j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
}
var _uid='<?php echo $uid; ?>';
var _uPhone='';
var _userDan={};
var _userTian={};
var _curId=0;
//_curId=116; //tests
var _leftTime=0;
$(function(){
  $(".p1").show();
  //$(".dan").show();
  $('.container, .page').width(sWidth).height(h);
  $('.p-b').height(_rate*1206);
  $('.p1-t1').css({"top":_rate*72});
  $('.p1-bmin').width(_rate*490).height(_rate*164).css({"top":_rate*610, "left":_rate*123});
  $('#zhu-name').width(_rate*368).height(_rate*76).css({"top":_rate*242, "left":_rate*278});
  $('#zhu-phone').width(_rate*368).height(_rate*76).css({"top":_rate*340, "left":_rate*278});
  $('#zhu-nianji').width(_rate*368).height(_rate*76).css({"top":_rate*438, "left":_rate*278}); 
  $('#zhu-idcard').width(_rate*368).height(_rate*76).css({"top":_rate*540, "left":_rate*278});
  $('#zhu-qu').width(_rate*368).height(_rate*76).css({"top":_rate*648, "left":_rate*278});
  $('#zhu-school').width(_rate*368).height(_rate*76).css({"top":_rate*750, "left":_rate*278});
  $('.zhu-ok').width(_rate*480).height(_rate*130).css({"top":_rate*950, "left":_rate*142});
  $('.dan-b1').height(_rate*25).css({"top":_rate*88, "left":_rate*72});
  $('.dan-b1-y').width(_rate*586);
  $('.dan-b1-x').width(0);
  
  $('.dan-title, .tian-title').css({"top":_rate*134});
  $('.tian-notice').css({"top":_rate*230});

  $('.up-btn').width(_rate*290).css({"left":_rate*80});
  $('.next-btn').width(_rate*290).css({"left":_rate*382});
  $('.dan-up, .dan-next').css({"top":_rate*1030});
  $('.tian-up, .tian-next').css({"top":_rate*630});

  $('.yib-abcd').css({"top":_rate*460});
  $('.yib-li').height(_rate*100).css({"line-height":_rate*55+'px','margin-bottom':_rate*6});
  $('.dx-li-ico').width(_rate*44).height(_rate*44).css({"top":_rate*4, "left":0});
  $('.yib-li p').css({"margin-left":_rate*66});
  $('.time-jiao, .gongxi-ok').width(_rate*440).height(_rate*160).css({"top":_rate*310, "left":_rate*160});
  $('.notice-ok').width(_rate*480).height(_rate*140).css({"top":_rate*940, "left":_rate*140});
  $('.notice-t3').width(_rate*686).height(_rate*630).css({"top":_rate*280});
  $('.notice-t3 img').css({"margin-top":-_rate*60});
  $('.tian input').width(_rate*160);
  
 
  if (sWidth < 340) {
     $('.yib-abcd').css({"top":_rate*500});
  }
    
  if(h >= _rate*1333){
        var _cha=h - _rate*1340;
        if(_cha < 0){_cha=0;}
        $('.thebg').css({"top":_cha/2});    
  }

  var _shufCookie50=getke('sz_shufCookie5');
  var _shufCookie120=getke('sz_shufCookie12');

  if('' == _shufCookie50){
      var _shuffle50=[];
      for(var i=1;i<=50;i++){
        _shuffle50.push(i);
      }
      _shuffle50.shuffle();
      setKe('sz_shufCookie5', _shuffle50);
  }else{
     var _shuffle50=_shufCookie50.split(",");
  }

  var _shuffle100=[];
  for(var i=1;i<=100;i++){
    _shuffle100.push(i);
  }
  var _shuffle200=[];
  for(var i=101;i<=200;i++){
    _shuffle200.push(i);
  }
  _shuffle100.shuffle();
  _shuffle200.shuffle();

  if('' == _shufCookie120){
      var _shuffle120=[];
      for(var i=1;i<=60;i++){
        _shuffle120.push(_shuffle100[i-1]);
      }
      for(var i=1;i<=60;i++){
        _shuffle120.push(_shuffle200[i-1]);
      }
      setKe('sz_shufCookie12', _shuffle120);
  }else{
     var _shuffle120=_shufCookie120.split(",");
  }
  
   
  console.log(_shuffle120);
 
  var _showId=_shuffle120[_curId];
  $(".dan-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/120）");
  $(".yib-bt, .yib-abcd").hide();
  $(".yb1-bt"+_showId+", .yb1-t"+_showId).show();

var _userCurStepCookie=getke('sz_userCurStepA'); //当前步骤
var _userIdCookie=getke('sz_userId');
var _userPhoneCookie=getke('sz_phone');
var _userCountEndTime=getke('sz_endTime');
var _userNianJiCookie=getke('sz_userNianJi');

if (typeof(_userIdCookie) != "undefined" && '' != _userIdCookie) {
   _uid=_userIdCookie;
}
if (typeof(_userPhoneCookie) != "undefined" && '' != _userPhoneCookie) {
   _uPhone=_userPhoneCookie;
}
if(typeof(_shufCookie50) != "undefined" && '' != _shufCookie50 && '' != _userPhoneCookie && '' != _userIdCookie){  //记忆
  if('dan' == _userCurStepCookie || 'tian' == _userCurStepCookie){
     var _tmDataStoreCookie=getke('sz_tmData');
     var _tmKeyStoreCookie=getke('sz_tmKey');

     if("" != _tmDataStoreCookie){
        var _tmpTmData=_tmDataStoreCookie.split(",");
        var _tmpTmKey=_tmKeyStoreCookie.split(",");
        for(var _i=0;_i<_tmpTmData.length-1;_i++){
           _userDan[_tmpTmKey[_i]]=_tmpTmData[_i];
           _curId++;
        }
      }
       $(".yib-bt, .yib-abcd").hide();
       $(".dan-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/120）");
       var _showId=_shuffle120[_curId];
       $(".yb1-bt"+_showId+", .yb1-t"+_showId).show();
       
       for(var _i=0;_i<_curId;_i++){
          _showId=_shuffle120[_i];
          if(typeof(_userDan[_showId]) != "undefined"){
             $(".yb1-t"+_showId+" .dx-li-ico").attr("src","<?php echo $url; ?>/img/c.png");
             $(".yb1-t"+_showId+" .dx-li-"+_userDan[_showId]).attr("src","<?php echo $url; ?>/img/d.png"); 
          }
        }

     $(".p1").hide();
     $(".dan").show();
     _startCountTime();
    
     _curId--;

  }
  if('tian' == _userCurStepCookie){
      //初始化单选题
      _curId=0;

        var _tmDataStoreCookie=getke('sz_tmData2');
        var _tmKeyStoreCookie=getke('sz_tmKey2');
        if("" != _tmDataStoreCookie){
          var _tmpTmData=_tmDataStoreCookie.split(",");
          var _tmpTmKey=_tmKeyStoreCookie.split(",");
          for(var _i=0;_i<_tmpTmData.length;_i++){
             _userTian[_tmpTmKey[_i]]={1:decodeURI(_tmpTmData[_i])};
             _curId++;
          }
        }

       _showId=_shuffle50[_curId];


       $(".yib-bt, .yib-abcd").hide();
       $(".tian-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/30）");
       $(".tian-bt").hide();
       $(".tian-t"+_showId).show();
       for(var _i=0;_i<_curId;_i++){
        _showId=_shuffle50[_i];
        if (typeof(_userTian[_showId]) != "undefined" && typeof(_userTian[_showId][1]) != "undefined" ) {
             $(".tian-t"+_showId+" .tk-1").val(_userTian[_showId][1]);
        }
      }
 
     $(".p1, .dan").hide();
     $(".tian").show();
  }


}
      
   

  $(".zhu-ok").click(function(){
     var _name=$("#zhu-name").val();
     var _phone=$("#zhu-phone").val();
     var _nianji=$("#zhu-nianji").val();
     var _idcard=$("#zhu-idcard").val();
     var _qu=$("#zhu-qu").val();
     var _school=$("#zhu-school").val();
    
     if('' == _name){
         _popMsg("请填写学生姓名");
           return;  
     }
     if('' == _phone){
         _popMsg("请填写可联系的真实手机号码");
           return;  
     }
     if(_phone.length < 6){
        _popMsg("请填写可联系的真实手机号码");
        return;  
     }
     if(_phone.length != 11){
        _popMsg("请填写11位的手机号码");
        return;  
     }
     if('' == _nianji){
         _popMsg("请选择年级");
         return; 
     }
      
     if('' == _idcard){
         _popMsg("请填写学生本人的身份证");
         return; 
     }
     if(_idcard.length < 4){
        _popMsg("请填写真实的身份证");
        return;  
     }
     
    if('' == _qu){
         _popMsg("请选择所在区");
         return; 
     }
     if('' == _school){
         _popMsg("请填写所在学校的全称");
         return; 
     }
     _uPhone=_phone;
    $.post("./api/?c=setu", {uid:_uid,n:_name,ph:_phone,nianji:_nianji,idc:_idcard,qu:_qu,school:_school}, function(data){
          if('y' == data.s){
              setKe('sz_phone', _phone); 
              setKe('sz_userId', _uid);
              setKe('sz_userNianJi', _nianji);
              _userNianJiCookie=_nianji;
              $(".zhu").hide();
              $(".notice").show();
              setKe('sz_userCurStepA', 'zhu');
          }
    }, "json");

  });
  
  $(".p1-bmin").click(function(){
    var _phone='';
 console.log(_userPhoneCookie);
    if(typeof(_userPhoneCookie) != "undefined" && '' != _userPhoneCookie){  //记忆
        _phone=_userPhoneCookie;
    }
    if(typeof(_userCurStepCookie) != "undefined" && 'end' == _userCurStepCookie){  //记忆
        _popMsg("您已参与过考试");
        return;
    }
    console.log(_phone);
    if ('' == _phone) {
      $(".p1").hide();
      $(".zhu").show();
    }
    $.post("./api/?c=getu", {uid:_uid,ph:_phone}, function(data){
          console.log(data);
          if('y' == data.s){
              if(2 == data.h){
                 _popMsg("您已参与过考试");
                return;
              }
            $(".p1").hide();
            $(".notice").show();  
          }else{
            $(".p1").hide();
            $(".zhu").show();
          }
    }, "json");
     
  });
  $(".gongxi-ok").click(function(){ 
      window.location.reload();
  });
  
  function _popMsg(msg){
    $(".weui-msg").html(msg);
        $(".js_dialog").show();
  }   
  $(".weui-btn").click(function(){
        $(".js_dialog").hide();
  });

  function _resetABCD(_type, _showId){
      _userDan[_showId]=_type;
      console.log(_userDan);
       var _tmData=[];
       var _tmKey=[];  
       $.each(_userDan,function(key,value) {
            _tmKey.push(key);
            _tmData.push(value);
        }); 
        setKe('sz_tmData', _tmData);
        setKe('sz_tmKey', _tmKey);
        setKe('sz_userCurStepA', 'dan');
      
      $(".yb1-t"+_showId+" .dx-li-ico").attr("src","<?php echo $url; ?>/img/c.png");
      $(".yb1-t"+_showId+" .dx-li-"+_type).attr("src","<?php echo $url; ?>/img/d.png");
 }

  $(".yib-abcd .dx-a").click(function(){
       _showId=_shuffle120[_curId];
       console.log(_showId);
       _resetABCD('a', _showId);
        
   });
   $(".yib-abcd .dx-b").click(function(){
       _showId=_shuffle120[_curId];
       _resetABCD('b', _showId);
   });
   $(".yib-abcd .dx-c").click(function(){
       _showId=_shuffle120[_curId];
       _resetABCD('c', _showId);
        
   });
   $(".yib-abcd .dx-d").click(function(){
      _showId=_shuffle120[_curId];
      _resetABCD('d', _showId);
   });

  $('.dan-next').click(function(){
        _curId++;
        if(_curId > 119){

          $.post("./api/?c=setdan", {yi:_userDan,op:_uid,phone:_uPhone,leftTime:_leftTime,nianji:_userNianJiCookie}, function(data){
              // $(".tipchen").show();
               //setKe('sz_userCurStepA', 'end');  
          }, "json");
          _curId=0; //填空初始化
          _showId=_shuffle50[_curId];

          $(".tian-t"+_showId).show();
          $(".tian-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/30）");
          $(".dan").hide();
          $(".tian").show();
           console.log('提交+end'); 
           return;
        }
        
        $(".yib-bt, .yib-abcd").hide();
        $(".dan-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/120）");
        var _showId=_shuffle120[_curId];
        $(".yb1-bt"+_showId+", .yb1-t"+_showId).show();
              
        $(".next-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");
        window.setTimeout(function(){
          $(".next-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");
        }, 200);
        setKe('sz_userCurStepA', 'dan');
         
   });

  $('.dan-up').click(function(){
      if(0 == _curId){return;}
      if(_curId < 0){return;}
       _curId--;
       $(".dan-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/120）");
       var _showId=_shuffle120[_curId];

       $(".yib-bt, .yib-abcd").hide();
       $(".yb1-bt"+_showId+", .yb1-t"+_showId).show();
       
        
       $(".up-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");
       window.setTimeout(function(){
          $(".up-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");
           
        }, 200);
       setKe('sz_userCurStepA', 'dan');
  
   });


  
    
    $('.tian-next').click(function(){
        _curId++;
        $(".tian-notice").hide();
        var _showId=_shuffle50[_curId-1];
        var _txtA=$(".tian .tian-t"+_showId+' .tk-1').val();
        if ('' != _txtA) {
            _userTian[_showId]={1:_txtA};
           var _tmData=[];
           var _tmKey=[];  
           $.each(_userTian,function(key,value) {
                _tmKey.push(key);
                _tmData.push(encodeURI(value[1]));
            }); 
            setKe('sz_tmData2', _tmData);
            setKe('sz_tmKey2', _tmKey);
        }
        setKe('sz_userCurStepA', 'tian');
        _showId=_shuffle50[_curId];
        
         
         if(_curId > 28){
            $(".next-btn-t").attr("src","<?php echo $url; ?>/img/next-btn2.png");
            $(".next-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");  
        }
        if(_curId > 29){
          $.post("./api/?c=settian", {kong:_userTian,op:_uid,phone:_uPhone,leftTime:_leftTime,nianji:_userNianJiCookie}, function(data){
               $(".gongxi").show();
               setKe('sz_userCurStepA', 'end');  
          }, "json");
           console.log('提交+end'); 
           return;
        }
         
        
        $(".yib-bt, .yib-abcd").hide();
        $(".tian-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/30）");
        
         $(".tian-bt").hide();
         $(".tian-t"+_showId).show();
              
        $(".next-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");
        window.setTimeout(function(){
          $(".next-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");
        }, 200);
         
   });

   $('.tian-up').click(function(){
    console.log(_curId);
      if(0 == _curId){ //返回单选
      _curId=119;
      $(".tian").hide();
      $(".dan").show();
       $(".dan-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/120）");
       var _showId=_shuffle120[_curId];
       $(".yib-bt, .yib-abcd").hide();
       $(".yb1-bt"+_showId+", .yb1-t"+_showId).show();
       $(".up-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");
       window.setTimeout(function(){
          $(".up-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");
        }, 200);
       setKe('sz_userCurStepA', 'dan');
        return;
      }
      if(1 == _curId){$(".tian-notice").show();}
      if(_curId < 0){return;}
       _curId--;
       $(".tian-title").html("第"+chinanum((_curId+1))+"题（"+(_curId+1)+"/30）");
       var _showId=_shuffle50[_curId];
        $(".tian-bt").hide();
        $(".tian-t"+_showId).show();
       $(".up-btn-h").attr("src","<?php echo $url; ?>/img/r-btn.png");
       $(".next-btn-t").attr("src","<?php echo $url; ?>/img/next-btn.png");
      $(".next-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");  
       window.setTimeout(function(){
          $(".up-btn-h").attr("src","<?php echo $url; ?>/img/h-btn.png");
        }, 200);
      setKe('sz_userCurStepA', 'tian');
  
   });
   
   $('.time-jiao').click(function(){
       var _userCurStepCookie=getke('sz_userCurStepA');
       if('tian' == _userCurStepCookie){
          $.post("./api/?c=settian", {kong:_userTian,op:_uid,phone:_uPhone, leftTime:_leftTime,nianji:_userNianJiCookie}, function(data){
               $(".gongxi").show();
               setKe('sz_userCurStepA', 'end');  
          }, "json");
       }
       if('dan' == _userCurStepCookie){
          $.post("./api/?c=setdan", {yi:_userDan,op:_uid,phone:_uPhone, leftTime:_leftTime,nianji:_userNianJiCookie}, function(data){
              $(".gongxi").show();
               setKe('sz_userCurStepA', 'end');   
          }, "json");
       }

        console.log(111);
   });

   $('.notice-ok').click(function(){
       $(".notice").hide();
       $(".dan").show();
       _startCountTime();
   });

  var end;
  function _startCountTime(){
    var endDate = new Date();
    var min=endDate.getMinutes();
    endDate.setMinutes(min+20);
    //endDate.setMinutes(min+1); test
    end = endDate.getTime()+900;
    if (typeof(_userCountEndTime) != "undefined" && '' != _userCountEndTime) {
        var date = new Date();  
        var now = date.getTime();
        if ((_userCountEndTime-now) >= 92000){
            end=_userCountEndTime;
        }
    }
    countTime();
    setKe('sz_endTime', end);
  }
  var _zongwidth=_rate*586;
  var _zongTime=60*20*1000;
  function countTime(){
    var date = new Date();  
    var now = date.getTime();
    _leftTime= end-now; 
    var m,s; 
    if (_leftTime >= 0){
      m = Math.floor(_leftTime/1000/60%60);
      s = Math.floor(_leftTime/1000%60);
      var _tmpS=':'+s;
      if(s<10){
        _tmpS=':0'+s;
      }
      //console.log(_leftTime);
      var _percent=1 - _leftTime/_zongTime + 0.02;
      if(_percent>1){_percent=1;}
      
      $(".dan-b1-x").width(_percent*_zongwidth);
      

      $(".dan-time").html(m + _tmpS );
      setTimeout(countTime,1000); 
    }else{
      $(".dan-time").html("00:00");
      _tmTimeId=0;
      $(".time").show();
      console.log('end');
    }
  }


  //  
});
</script>